<template>
  <div class="top-head">
    <div class="banxin flex">
      <div class="welcom">欢迎来到叩丁严选</div>
      <div class="right flex">
        <ul class="flex">
          <li>
            <img :src="userInfo.headImg" alt="" />
            <span>用户名：</span>
            <span>{{ userInfo.nickName }}</span>
          </li>
          <li>
            <span>我的鸡腿： </span>
            <span>{{ userInfo.coin }}</span>
          </li>
          <li>
            <span>获取鸡腿</span>
          </li>
          <li>
            <span>叩丁狼官网</span>
          </li>
        </ul>
        <div
          v-show="!islogin"
          class="btn"
          @click="$store.commit('modifyShowLogin', true)"
        >
          登录
        </div>
        <div v-show="islogin" class="btn" @click="$router.push('/personal/cart')">
          <img src="../../assets/images/Shopping.png" alt="" />
          <span>购物车</span>
          <span class="total">{{ cartTotal }}</span>
        </div>
      </div>
    </div>
  </div>
</template>
 
<script>
import { getUserInfoApi, WXloginApi } from "../../http/api.js";
import headImg from "../../assets/images/userImg.png";
import { toast } from "../../utils/toast.js";

export default {
  data() {
    return {
      userInfo: {
        headImg,
        nickName: "--",
        coin: "--",
      },
      cartTotal: 0,
      islogin: false,
    };
  },
  created() {
    this.getUerInfo();
    this.weix()
     
  },
  methods: {
    async getUerInfo() {
      const token = localStorage.getItem("x-auth-token");
      
      if (!token) return;
      this.islogin = true;
      const res = await getUserInfoApi();
      // console.log(res);
      if (res.code === 0) {
        this.userInfo = res.data.userInfo;
        this.cartTotal = res.data.cartTotal;
        // console.log(this.userInfo);
        // 把用户的信息保存到vuex里面
        this.$store.commit("updataUserInfo", this.userInfo);
      }
    },
    weix(){
              const code = this.$route.query.code;
              
              
        if (code) {
          WXloginApi(code).then((res) => {
            // console.log(res["x-auth-token"]);
            localStorage.setItem("x-auth-token", res["x-auth-token"]);
            toast("微信登录成功", "success");
            this.getUerInfo();
            // 把code从路由里面清除掉
            if(this.$route.path==='/gooddetail'){
              this.$$router.push(this.$route.path+'?id='+localStorage.getItem('detailId'))
            }else{
              this.$router.push(this.$route.path)
            }
          });
        }
    }
  },
  watch: {
    "$route.query": {
      handler() {
        this.weix()
      },
    },
  },
};
</script>
 
<style lang = "less" scoped>
.top-head {
  color: #fff;
  font-size: 14px;
  height: 40px;
  line-height: 40px;
  background: #242b39;
  .banxin {
    justify-content: space-between;
  }
  .right {
    ul {
      li {
        cursor: pointer;
        margin-right: 20px;
        img {
          width: 20px;
          height: 20px;
          vertical-align: middle;
          margin-right: 5px;
          border-radius: 50%;
        }
      }
    }
    .btn {
      cursor: pointer;
      width: 124px;
      text-align: center;
      background: #0a328e;
    }
  }
}
</style>